<template>
  <el-card class="box-card" >
    <div slot="header" class="clearfix">
      <breadcrumb>添加文章</breadcrumb>
    </div>
    <el-form ref="refForm" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容：" class="publish-container" prop="content">
        <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="form.coverType" @change="hImageTypeChange">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>
        <!-- 实现封面组件的引用
          只有是1和3的时候才显示出来-->
        <el-row :gutter="50" class="img_list">
          <el-col v-for="(item, idx) in form.coverType"
            :key="idx" :xs="12" :sm="6" :md="6" :lg="4">
            <cover v-model="form.coverImages[idx]"></cover>
          </el-col>
        </el-row>
      </el-form-item>
      <channel v-model="form.channelId"></channel>
      <el-form-item>
        <el-button type="primary" @click.native="hPublish(false)" :loading="form.isloading">发表</el-button>
        <el-button type="primary" @click.native="hPublish(true)" :loading="form.isloading">存入草稿</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
// 1.实现布局，引用富文本编辑器---完成
// 2.发送请求，完成发布功能---完成
// 3.封装封面组件---
// (1)完成路由配置测试---完成
// (2)完成上传图片按钮布局，点击弹处框---完成
// (3)素材库和上传图片的页面布局并实现上传功能---完成
// (4)添加伪类实现选中的效果---完成
// (5)在父组件中引用---
// (6) 确认图片---完成
// (7)处理默认的图片效---完成
// (8) 封面组件的v-model数据双向绑定---完成
// 4.发布文章进行表单验证---完成

// 引入富文本编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  name: 'addatical',
  data () {
    return {
      // 富文本配置对象
      editorOption: {
      // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      draft: false,
      form: {
        title: '遇见未知的自己',
        content: '在某个秋日的下午，泡一杯咖啡，安静的看着书，悠然自在',
        cover: '',
        coverType: 0,
        channelId: '',
        coverImages: [],
        isloading: false
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    hImageTypeChange () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType === 3) {
        this.form.coverImages.length = 3
      }
    },
    // 发布文章前进行表单的验证
    hPublish (draft = false) {
      this.$refs.refForm.validate(valid => {
        if (valid) {
          this.doPublish(draft)
        } else {
          this.$message.error('请将文章信息补全')
        }
      })
    },
    async doPublish (draft) {
      // 修改isLoading的值
      this.form.isloading = true
      try {
        // 1.解构赋值
        const { title, content, channelId, coverType, coverImages } = this.form
        // 2.发送axios请求
        const res = await this.$http({
          method: 'POST',
          url: '/mp/v1_0/articles',
          params: {
            draft
          },
          data: {
            title,
            content,
            cover: {
              type: coverType,
              images: coverImages
            },
            channel_id: channelId
          }
        })
        console.log('文章发布成功', res)
        // 3.发布成功，消息提示
        this.$message.success('发布文章成功')
        // 4.修改isloading的值
        this.form.isloading = false
      } catch (err) {
        this.$message.error('发布文章失败')
        this.form.isloading = false
      }
    }
  }
}
</script>
<style leng="less" scoped>
  .text {
    font-size: 14px;
  }
  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 100%;
  }
  .publish-container /deep/ .ql-editor{
  height: 300px;
  }
</style>
